﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace HelloASPX
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["username"] != null)
                {
                    Response.Cookies["username"].Value = HttpContext.Current.Request.Cookies["username"].Value;
                    Response.Redirect("Default.aspx");
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            
            if (Request.Cookies["CheckCode"].Value.Equals(yzm.Text))
            {


            }
            else
            {
                yzmLabel.Text = "验证码错误"+ Request.Cookies["CheckCode"].Value;
                return;
            }

            if (username.Text == "")
            {
                return;
            }
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/user.xml"));
            XmlNode usernode = doc.SelectSingleNode("/users/user[username='" + username.Text + "']");


            if (AutoLogin.Checked)
            {
                HttpCookie cookie = new HttpCookie("username");
                cookie.Value = username.Text;
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = new HttpCookie("username");
                cookie.Value = "";
                HttpContext.Current.Response.Cookies.Add(cookie);
            }

            if (usernode.SelectSingleNode("mima").InnerText.Equals(psd.Text))
            {
                Session["username"] = username.Text;
                Response.Redirect("ProductList.aspx");

                //Response.Cookies["username"].Value = username.Text;
                //Response.Write("<script>alert('登录成功！');location='ProductDetail.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('登录失败，用户名或密码错误！');</script>");
            }

        }

        protected void username_TextChanged(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/user.xml"));
            XmlNodeList usernodes = doc.SelectNodes("//user[username='" + username.Text + "']");
            if (usernodes.Count > 0)
            {
                usernameLabel.Text = "";
            }
            else
            {
                usernameLabel.Text = "该用户名不存在！";
            }
        }

        protected void psd_TextChanged(object sender, EventArgs e)
        {

        }

        protected void yzm_TextChanged(object sender, EventArgs e)
        {

        }

        protected void AutoLogin_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}